package com.cy.jt.demo.service;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class MemoryService {

    //private Map<Object,Map<Object,Object>> cacheMap;

    //本地缓存(JVM内部的缓存)   数据获取后保存在本地缓存中，以后查找时快速获取，提高查询效率
    //private Map<String,Object> cache =new ConcurrentHashMap<>();
    @Cacheable(value="myCache")   //该注解表示的方法为缓存切入点方法
    public List<Map<String,Object>> list(){
      /*  if(cache.containsKey("memoryKey")){   //实际上使用时这个key是参数列表的集合
            return (List<Map<String, Object>>) cache.get("memoryKey");
        }*/
        System.out.println("Get Data from Database");
        //假设如下数据来自于数据库
        Map<String,Object> m1=new HashMap<>();
        m1.put("id",100);
        m1.put("title","title-A");
        Map<String,Object> m2=new HashMap<>();
        m2.put("id",101);
        m2.put("title","title-B");
        List<Map<String,Object>> list=new ArrayList<>();
        list.add(m1);
        list.add(m2);
        //cache.put("id", 101);
        return list;
      }
}
